//
//  XNViewController.m
//  核心动画
//
//  Created by liuyanqi on 2018/6/6.
//  Copyright © 2018 linch. All rights reserved.
//

#import "XNViewController.h"

@interface XNViewController ()

{
    CGPoint pointStart;
    UIImageView *imgvR;
    CGPoint center;
    double huduAll;
    double hudu;
    NSInteger direction;
    NSMutableArray *arrayPoint;
}
@end

@implementation XNViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    huduAll = 0;
    direction = 1;
    self.title = @"旋钮动画";
    self.view.backgroundColor = [UIColor whiteColor];
    arrayPoint = [[NSMutableArray alloc]init];
    [arrayPoint addObject:@"1"];
    [arrayPoint addObject:@"1"];
    [self interfaceBuild];
    
}

- (void)interfaceBuild
{
    
    center = self.view.center;
    
    UIImageView *imgvbg = [[UIImageView alloc]init];
    imgvbg.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:imgvbg];
    imgvbg.layer.cornerRadius = 125;
    imgvbg.clipsToBounds = YES;
    imgvbg.center = center;
    imgvbg.bounds = CGRectMake(0, 0, 250, 250);
    imgvbg.userInteractionEnabled = YES;
    UIPanGestureRecognizer *gest = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(moveRound:)];
    [imgvbg addGestureRecognizer:gest];
    
    UIImageView *imageView = [[UIImageView alloc]init];
    [self.view addSubview:imageView];
    imageView.image = [UIImage imageNamed:@"max1"];
    imageView.center = center;
    imageView.bounds = CGRectMake(0, 0, 210, 210);
    
    UIImageView *imgvW = [[UIImageView alloc]init];
    imgvW.image = [UIImage imageNamed:@"max2"];
    [self.view addSubview:imgvW];
    
    //大半径  125   小  105   中  115
    CGFloat imgx = center.x + cos(M_PI * 5/3) * 115;
    CGFloat imgy = center.y - sin(M_PI * 5/3) * 115;
    imgvW.center = CGPointMake(imgx, imgy);
    imgvW.bounds = CGRectMake(0, 0, 7.5, 12);
    
    
    imgvR =  [[UIImageView alloc]init];
    imgvR.image = [UIImage imageNamed:@"max3"];
    [self.view addSubview:imgvR];
    CGFloat imgrx = center.x + cos(M_PI * 4/3) * 115;
    CGFloat imgry = center.y - sin(M_PI * 4/3) * 115;
    imgvR.center = CGPointMake(imgrx, imgry);
    imgvR.bounds = CGRectMake(0, 0, 7.5, 12);
    
    
    
    UIButton *buttonPop = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:buttonPop];
    buttonPop.frame = CGRectMake(10, self.view.frame.size.height - 60, self.view.frame.size.width -20, 50);
    [buttonPop addTarget:self action:@selector(popMethod) forControlEvents:UIControlEventTouchUpInside];
    buttonPop.backgroundColor = [UIColor orangeColor];
    [buttonPop setTitle:@"BACK" forState:UIControlStateNormal];
    
    
}


- (void)moveRound:(UIPanGestureRecognizer *)gesture
{
    //    NSLog(@"============");
    CGPoint pointG =[gesture locationInView:self.view];
    CGPoint velocity = [gesture velocityInView:self.view];
    if(velocity.x>0 && velocity.y<0 && pointG.x<center.x && pointG.y<center.y)
    {
        NSLog(@"----------2+");
        direction = 1;
    }else if(velocity.x<0 && velocity.y>0 && pointG.x<center.x && pointG.y<center.y)
    {
        NSLog(@"----------2-");
        direction = -1;
    }else if(velocity.x>0 && velocity.y>0 && pointG.x>center.x&& pointG.y<center.y)
    {
        NSLog(@"----------1+");
        direction = 1;
    }else if(velocity.x<0 && velocity.y<0 && pointG.x>center.x&& pointG.y<center.y)
    {
        NSLog(@"----------1-");
        direction = -1;
    }else if(velocity.x<0 && velocity.y>0 && pointG.x>center.x&& pointG.y>center.y)
    {
        NSLog(@"----------4+");
        direction = +1;
    }else if(velocity.x>0 && velocity.y<0 && pointG.x>center.x&& pointG.y>center.y)
    {
        NSLog(@"----------4-");
        direction = -1;
    }else if(velocity.x<0 && velocity.y<0 && pointG.x<center.x&& pointG.y>center.y)
    {
        NSLog(@"----------3+");
        direction = 1;
    }else if(velocity.x>0 && velocity.y>0 && pointG.x<center.x&& pointG.y>center.y)
    {
        NSLog(@"----------3-");
        direction = -1;
    }
    switch (gesture.state) {
        case UIGestureRecognizerStateBegan:
        {
            pointStart = [gesture locationInView:self.view];
            [arrayPoint replaceObjectAtIndex:1 withObject:[NSValue valueWithCGPoint:pointStart]];
            
            break;
        }
            
        case UIGestureRecognizerStateChanged:
        {
            CGPoint pointNew = [gesture locationInView:self.view];
            [arrayPoint exchangeObjectAtIndex:0 withObjectAtIndex:1];
            [arrayPoint replaceObjectAtIndex:1 withObject:[NSValue valueWithCGPoint:pointNew]];
            [self animationMethod:arrayPoint];
            
            break;
        }
        case UIGestureRecognizerStateEnded:
        {
            [self endMove];
            break;
        }
        default:
            break;
    }
    
}
- (void)animationMethod:(NSMutableArray *)array
{
    if ([array[1] isKindOfClass:[NSValue class]]&&[array[0] isKindOfClass:[NSValue class]]) {
        
        NSValue *valueE = array[1];
        NSValue *valueS = array[0];
        
        CGPoint pointS = [valueS CGPointValue];
        CGPoint pointE = [valueE CGPointValue];
        
        if (pointS.y == center.y && pointE.y == center.y) {
            //水平
            return;
        }
        if (pointS.x == center.x && pointE.x == center.x) {
            //竖直
            return;
        }
        double xls = (pointS.y-center.y)/(pointS.x - center.x);
        double xle = (pointE.y-center.y)/(pointE.x - center.x);
        if (xls == xle ){
            
             NSLog(@"===============%f=%f==============%f==%f",pointS.x,pointS.y,pointE.x,pointE.y);
            
            return;
        }
        
        
        double ls2 = (pointS.x-center.x)*(pointS.x-center.x) + (pointS.y-center.y)*(pointS.y-center.y);
        double le2 = (pointE.x-center.x)*(pointE.x-center.x) + (pointE.y-center.y)*(pointE.y-center.y);
        double se2 = (pointE.x-pointS.x)*(pointE.x-pointS.x) + (pointE.y-pointS.y)*(pointE.y-pointS.y);
        double ls = sqrt(ls2);
        double le = sqrt(le2);
        if (se2 == 0) {
            
            return;
        }
        double hudu = acos((le2 + ls2 - se2)/(2 * ls * le));

        huduAll = huduAll +direction *hudu;
        
        if (huduAll>M_PI * 5/3 ) {
            huduAll = M_PI * 5/3;
            return;
        }
        if (huduAll<0) {
            huduAll = 0;
            return;
        }
       
        NSLog(@"--------%f",huduAll);
        imgvR.transform = CGAffineTransformMakeRotation(huduAll);
        CGFloat imgrx = center.x + cos(M_PI*4/3 - huduAll) * 115;
        CGFloat imgry = center.y - sin(M_PI*4/3 - huduAll) * 115;
        
        imgvR.center = CGPointMake(imgrx, imgry);
        
        
    }
}
-(void)endMove
{
    [arrayPoint replaceObjectAtIndex:0 withObject:@"1"];
    [arrayPoint replaceObjectAtIndex:1 withObject:@"1"];
}
- (void)popMethod
{
    [self.navigationController popViewControllerAnimated:YES];
}

@end




